package com.th.supcom.pds.impl.common.utils;

import java.math.BigDecimal;
import java.util.Date;

import net.carefx.framework.container.client.context.AppContext;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.i18n.client.DateTimeFormat;

/**
 * 一些通用（简便）的封装方法（待添加）
 * 
 * @author liaoxianjun
 */
public final class SimpleMethod
{

    /** display消息显示 */
    public static void display (final Object msg)
    {
        Info.display ("提示", "" + msg);
    }

    /**
     * 时间格式化yyyy-MM-dd
     * 
     * @param date 传入的时间类型的参数
     * @return String
     * */
    public static String formatDate (final Date date)
    {
        Date date2 = new Date ();
        if (null != date)
        {
            date2 = date;
        }
        final DateTimeFormat format = DateTimeFormat.getFormat ("yyyy-MM-dd");
        final String dt = format.format (date2);
        return dt;
    }

    /**
     * 时间格式化
     * 
     * @param date 需要格式化的时间类型的参数
     * @param format 格式(如：yyyy-MM-dd)
     * @return String
     * */
    public static String formatDate (final Date date, final String format)
    {
        final DateTimeFormat formatDate = DateTimeFormat.getFormat (format);
        final String dt = formatDate.format (date);
        return dt;
    }

    /** 获取现金金额，没有分子钱 */
    public static String getCash (final String amount)
    {

        if (null == amount || "".equals (amount))
        {
            return "0.0";
        }
        if (!amount.contains ("."))
        {
            return amount;
        }
        final int index = amount.indexOf ('.');

        if (index == amount.length () - 2)
        {
            return amount;
        }

        return amount.substring (0, index + 2);
    }

    /**
     * 判断是否打印
     * 
     * @return pString preview,print
     */
    public static String getIsPrintView ()
    {
        final String isPreview = AppContext.get ().getConfiguration ().get ("bms/isPreview");
        final String pString = "true".equals (isPreview) ? "preview" : "print";
        return pString;
    }

    /** 获取带有两位小数的舍入金额，即舍入的分子钱 */
    public static Double getRoundAmount (final String amount)
    {
        Double round = 0.0d;
        if (null == amount || "".equals (amount))
        {
            return 0.0d;
        }
        if (!amount.contains ("."))
        {
            return 0.0d;
        }
        final int index = amount.indexOf ('.');
        if (index != amount.length () - 3)
        {
            return 0.0d;
        }
        final String str = amount.substring (amount.length () - 1, amount.length ());
        round = Double.parseDouble (str);
        if (round <= 4)
        {
            round *= -0.01;
        }
        else
        {
            round = (10 - round) * 0.01;
        }
        if (amount.contains ("-"))
        {
            round *= -1;
        }
        return round;
    }

    /** 只需要输入提示内容的info提示框 */
    public static void info (final Object msg)
    {
        MessageBox.info ("提示", "" + msg, null);
    }

    /** 只需要输入提示标题和提示内容的info提示框 */
    public static void info (final String title, final Object msg)
    {
        MessageBox.info (title, "" + msg, null);
    }

    /** 只需要输入提示内容的alert提示框 */
    public static void mbx (final Object msg)
    {
        MessageBox.alert ("提示", "" + msg, null);
    }

    /** 只需要输入提示标题和提示内容的alert提示框 */
    public static void mbx (final String title, final Object msg)
    {
        MessageBox.alert (title, "" + msg, null);
    }

    /** 给金额设置样式 */
    public static String moneyStyle (final Double db)
    {
        Double double1 = 0.0D;
        if (db != null)
        {
            double1 = db;
        }
        final String style = "<span style='color:red;font-size:12px;'><b>" + SimpleMethod.retainDoubleTwo (double1) + "</b></span>";
        return style;
    }

    /** 给lable设置样式 */
    public static String moneyStyle (final String info)
    {
        if (info == null)
        {
            SimpleMethod.mbx ("转入的font为空！");
        }
        final String style = "<span style='color:blue;font-size:12px;'><b>" + info + "：</b></span>";
        return style;
    }

    /**
     * double保留n位小数的四舍五入
     * 
     * @param douBle 传入需要转换的double
     * @return Double
     */
    public static Double retainDoubleNum (final Double douBle, final int newScale)
    {
        final BigDecimal bigDecimal = new BigDecimal (douBle);
        final Double d = bigDecimal.setScale (newScale, BigDecimal.ROUND_HALF_UP).doubleValue ();
        return d;
    }

    /**
     * double保留2位小数
     * 
     * @param douBle 传入需要转换的double
     * @return Double
     */
    public static Double retainDoubleTwo (final Double douBle)
    {
        Double db = 0.00D;
        if (douBle != null)
        {
            db = douBle;
        }
        final BigDecimal bigDecimal = new BigDecimal (db);
        final Double d = bigDecimal.setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
        return d;
    }

    /**
     * 初始化列方法
     */
    public static ColumnConfig setColumnConfig (final ColumnConfig column, final String id, final String header, final HorizontalAlignment alignment, final Integer width,
                                                final GridCellRenderer < ? > renderer)
    {
        column.setId (id);
        column.setHeader (header);
        column.setAlignment (alignment);
        column.setWidth (width);
        column.setRenderer (renderer);
        return column;
    }

    /**
     * 给文本框设置样式
     * 
     * @param textField 需要设置的TextField <String>
     * @param fieldLabel 文本框fieldLabel名称
     * @return TextField <String>
     */
    public static TextField <String> textFieldStyle (final TextField <String> textField, final String fieldLabel)
    {
        textField.setFieldLabel (fieldLabel);
        textField.setLabelSeparator ("");
        textField.setReadOnly (true);
        textField.setInputStyleAttribute ("color", "blue");
        return textField;
    }

    /**
     * 左补零
     * 
     * @param param
     * @return String
     */
    public static String zeroFill (final Double param)
    {
        Double d = 0D;
        if (param != null)
        {
            d = param;
        }
        d = d * 100;
        final String s = String.valueOf (d.intValue ());
        final StringBuilder sb = new StringBuilder (12);
        for (int j = 0; j < 12 - s.length (); j++)
        {
            sb.append (0);
        }
        sb.append (s);
        return sb.toString ();
    }

    private SimpleMethod ()
    {
    }

    /**
     * 撤销上次的银联交易
     * 
     * @param money 需退的金额
     * @param sysNo 上次交易成功的流水号
     * @param editorPanel
     */
    // public static void backUnionpay (String money, String sysNo,
    // EMREditorPanel editorPanel)
    // {
    // UnionPayArgModel unionPayArgModel = new UnionPayArgModel ();
    // unionPayArgModel.setTrans ("01");
    // unionPayArgModel.setAmount (money);
    // unionPayArgModel.setOldTrace (sysNo);
    // UnionPayUtils m_unionPayUtils = new UnionPayUtils (editorPanel);
    // m_unionPayUtils.setUnionPayArgModel (unionPayArgModel);
    // m_unionPayUtils.executePayCommand ();
    //
    // }

}